/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package service;

import javax.ws.rs.Path;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.DELETE;
import javax.ws.rs.Produces;
import javax.ws.rs.Consumes;
import javax.ws.rs.QueryParam;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.WebApplicationException;
import javax.persistence.NoResultException;
import javax.persistence.EntityManager;
import jpa.entities.Endereco;
import jpa.entities.UsuarioCli;
import converter.EnderecoConverter;
import javax.ejb.Stateless;

/**
 *
 * @author carlos
 */

@Stateless
public class EnderecoResource {
    @javax.ejb.EJB
    private UsuarioCliResourceSub usuarioCliResourceSub;
    @Context
    protected UriInfo uriInfo;
    protected EntityManager em;
    protected Integer id;
  
    /** Creates a new instance of EnderecoResource */
    public EnderecoResource() {
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public void setEm(EntityManager em) {
        this.em = em;
    }

    /**
     * Get method for retrieving an instance of Endereco identified by id in XML format.
     *
     * @param id identifier for the entity
     * @return an instance of EnderecoConverter
     */
    @GET
    @Produces({"application/xml", "application/json"})
    public EnderecoConverter get(@QueryParam("expandLevel")
                                 @DefaultValue("1")
    int expandLevel) {
        return new EnderecoConverter(getEntity(), uriInfo.getAbsolutePath(), expandLevel);
    }

    /**
     * Put method for updating an instance of Endereco identified by id using XML as the input format.
     *
     * @param id identifier for the entity
     * @param data an EnderecoConverter entity that is deserialized from a XML stream
     */
    @PUT
    @Consumes({"application/xml", "application/json"})
    public void put(EnderecoConverter data) {
        updateEntity(getEntity(), data.resolveEntity(em));
    }

    /**
     * Delete method for deleting an instance of Endereco identified by id.
     *
     * @param id identifier for the entity
     */
    @DELETE
    public void delete() {
        deleteEntity(getEntity());
    }

    /**
     * Returns an instance of Endereco identified by id.
     *
     * @param id identifier for the entity
     * @return an instance of Endereco
     */
    protected Endereco getEntity() {
        try {
            return (Endereco) em.createQuery("SELECT e FROM Endereco e where e.idEndereco = :idEndereco").setParameter("idEndereco", id).getSingleResult();
        } catch (NoResultException ex) {
            throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
        }
    }

    /**
     * Updates entity using data from newEntity.
     *
     * @param entity the entity to update
     * @param newEntity the entity containing the new data
     * @return the updated entity
     */
    private Endereco updateEntity(Endereco entity, Endereco newEntity) {
        UsuarioCli usuarioCli = entity.getUsuarioCli();
        UsuarioCli usuarioCliNew = newEntity.getUsuarioCli();
        entity = em.merge(newEntity);
        if (usuarioCli != null && !usuarioCli.equals(usuarioCliNew)) {
            usuarioCli.getEnderecoCollection().remove(entity);
        }
        if (usuarioCliNew != null && !usuarioCliNew.equals(usuarioCli)) {
            usuarioCliNew.getEnderecoCollection().add(entity);
        }
        return entity;
    }

    /**
     * Deletes the entity.
     *
     * @param entity the entity to deletle
     */
    private void deleteEntity(Endereco entity) {
        UsuarioCli usuarioCli = entity.getUsuarioCli();
        if (usuarioCli != null) {
            usuarioCli.getEnderecoCollection().remove(entity);
        }
        em.remove(entity);
    }

    /**
     * Returns a dynamic instance of UsuarioCliResource used for entity navigation.
     *
     * @param id identifier for the parent entity
     * @return an instance of UsuarioCliResource
     */
    @Path("usuarioCli/")
    public UsuarioCliResource getUsuarioCliResource() {
        usuarioCliResourceSub.setParent(getEntity());
        return usuarioCliResourceSub;
    }

    @Stateless(name = "EnderecoResource.UsuarioCliResourceSub")
    public static class UsuarioCliResourceSub extends UsuarioCliResource {

        private Endereco parent;

        public void setParent(Endereco parent) {
            this.parent = parent;
        }

        @Override
        protected UsuarioCli getEntity() {
            UsuarioCli entity = parent.getUsuarioCli();
            if (entity == null) {
                throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
            }
            return entity;
        }
    }
}
